﻿// Copyright (c) 2011 Dan Popick

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

using Open.Web.Stijl.Css;
using Open.Web.Stijl.Utility;

namespace Open.Web.Stijl
{
	/// <summary>
	/// A tiled image which repeats the tiles to the full width of the image
	/// </summary>
	public class RepeatingTiledImage: TiledImage
	{
		static Tracer<RepeatingTiledImage> _log = new Tracer<RepeatingTiledImage>();

		public RepeatType RepeatStyle { get; private set; }
		public int Size { get; private set; }

		public RepeatingTiledImage(IEnumerable<Tile> tiles, ColorSet colors, RepeatType repeatStyle, int size)
			: base(tiles, colors)
		{
			RepeatStyle = repeatStyle;
			Size = size;

			if (RepeatStyle != RepeatType.RepeatX && RepeatStyle != RepeatType.RepeatY)
				throw new ArgumentOutOfRangeException("repeatStyle", "The repeat style must be RepeatX or RepeatY");
		}

		/// <summary>
		/// Gets the extent of the image, based on the repeat type and the sum of secondary sizes
		/// </summary>
		/// <returns>The full extent of the final image</returns>
		protected override Size GetImageDimensions()
		{
			Size s;
			if (RepeatStyle == RepeatType.RepeatX)
				s = new Size(
					Size,
					Tiles.Sum(x => x.Size.Height)
				);
			else
				s = new Size(
					Tiles.Sum(x => x.Size.Width),
					Size
				);

			_log.Debug("Dimensions for {0}: {1}x{2}", this.FileName, s.Width, s.Height);

			return s;
		}

		protected override void DrawTiledImage(System.Drawing.Graphics graphics, Tile tile)
		{
			Image im = tile.Sprite.GetImage();

			if (RepeatStyle == RepeatType.RepeatX)
			{
				_log.Debug("Drawing {0} ({1}x{2}) at {3} ({4}/{5})",
					tile.Sprite.Url, tile.Size.Width, tile.Size.Height,
					tile.Location.Y, tile.Sprite.Size.Width, Size);
				for (int i = 0; i < Size; i += tile.Sprite.Size.Width)
					graphics.DrawImage(tile.Sprite.GetImage(), new Rectangle(i, tile.Location.Y, im.Width, im.Height));
			}
			else
			{
				_log.Debug("Drawing {0} ({1}x{2}) at {3} ({4}/{5})",
					tile.Sprite.Url, tile.Size.Width, tile.Size.Height,
					tile.Location.X, tile.Sprite.Size.Height, Size);
				for (int i = 0; i < Size; i += tile.Sprite.Size.Height)
					graphics.DrawImage(tile.Sprite.GetImage(), new Rectangle(tile.Location.X, i, im.Width, im.Height));
			}
		}
	}
}
